package com.hzya.frame.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @Content
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2022-12-05 9:40
 * @Version 1.0
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    public static ApplicationContext getApplicationContext(){
        return context;
    }

    /**
     * 通过name获取 Bean
     * @param name beanName
     * @return Object
     */
    public static Object getBeanByName(String name){
        return getApplicationContext().getBean(name);
    }
    public static <T> Map<String, T> getBeansOfType(Class  beaNameClass){
        return getApplicationContext().getBeansOfType(beaNameClass);
    }

    public static <T> T getBeanByClass(Class<T> requiredType) throws BeansException {
        return getApplicationContext().getBean(requiredType);
    }

    public static Object getBeanByName2(String name){
        return getApplicationContext().getParentBeanFactory();
    }
}
